package com.atguigu.tingshu.search.strategy;


import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.search.repository.AlbumInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component("1001")
public class ProcessAlbumPurchases implements ProcessDiffTypePurchases {
    @Autowired
    private AlbumInfoRepository albumInfoRepository;

    /**
     * 处理专辑类型购买量(Es)
     * @param orderInfo 订单信息
     */
    @Override
    public void processTypePurchases(OrderInfo orderInfo) {
        Long albumId = orderInfo.getOrderDetailList().get(0).getItemId();
        Optional<AlbumInfoIndex> albumInfoIndexList = albumInfoRepository.findById(albumId);
        albumInfoIndexList.ifPresent(albumInfoIndex -> {
            Integer buyStatNum = albumInfoIndex.getBuyStatNum();
            albumInfoIndex.setBuyStatNum(buyStatNum + orderInfo.getOrderDetailList().size());
            albumInfoRepository.save(albumInfoIndex);
        });
    }
}